<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of petexpedition
 *
 * @author Dragon.hu add @2015.9.11
 */
defined('BASEPATH') or die('forbidden');

class Petexpedition_Controller extends Controller {

	private $inc;
	private $logdb;	//数据库连接
	private $platformList;	//平台列表
	
	function _init() {
		check_level(50, segment(0), segment(1), segment(2, 'index'));
		set_time_limit(0);
		
		$this->inc = $this->config['inc'];
		$this->platformList = $this->inc['platform_list'];
		
        // log数据库
		//$this->logdb = $this->load->database('log_db');
		//得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
		if ($areanum == '')
		{
			echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
			return;
		}
		// log数据库
		$this->logdb = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_LOG);
		
		if (false == $this->logdb) {
			//内部有提示错误，在此就不提示了
			 //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
		}	
	}

	function index() {
		$this->view->display("log_pet/petexpedition/index");
	}

	function flexigrid() {
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";

        $sql1 = "SELECT {$colsinfo} FROM gamelog_28 where {$where}  {$orderby} LIMIT {$offset},{$rp}";
        $sql2 = "SELECT {$colsinfo} FROM gamelog_29 where {$where}  {$orderby} LIMIT {$offset},{$rp}";
        $sql3 = "SELECT {$colsinfo} FROM gamelog_30 where {$where}  {$orderby} LIMIT {$offset},{$rp}";
        $sql4 = "SELECT {$colsinfo} FROM gamelog_31 where {$where}  {$orderby} LIMIT {$offset},{$rp}";
        $this->logdb->query($sql1);
        $result1=$this->logdb->row('default');
        $this->logdb->query($sql2);
        $result2=$this->logdb->row('default');
        $this->logdb->query($sql3);
        $result3=$this->logdb->row('default');
        $this->logdb->query($sql4);
        $result4=$this->logdb->row('default');
        $result5=array_merge($result1,$result2,$result3,$result4);
        $total=count($result5);

        foreach ($result5 as $k=>$n){
            $n['time']=date('Y/m/d H:i:s',$n['time']);
            $n['eventid']=$this->config['event'][$n['eventid']];
            $n['srcid']=$this->config['source'][$n['srcid']];
            $rows[] = array('id' => $n['id'], 'cell' => $n);
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );

        echo json_encode($result);
	}

}
